package shenfen;
import java.util.*;
import java.lang.String;
public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n;
        String xuan;
        while (in.hasNextInt())
        {
        	n=in.nextInt();
        	String []shen=new String[n];
		    for(int i=0;i<n;i++)
		    {
			   shen[i]=in.next();
		    }
            xuan =in.next();
        	String []birth=new String[n];
        	char []c=new char[8];
        	long []shuzi=new long[n];
        	String []hui=new String[n];
        	for(int i=0;i<n;i++)
        	{
        	   birth[i]=shen[i].substring(6, 14);
        	   shuzi[i]=Long.parseLong(birth[i]);
        	}
        	Arrays.sort(shuzi);
        	
        	if(xuan.equals("sort1"))
            {
        		for(int i=0;i<n;i++)
        		{
        			  hui[i]=Long.toString(shuzi[i]);
        			  
        		}
        	  for(int i=0;i<n;i++)
        	  {
        		c=hui[i].toCharArray();
			    for(int j=0;j<4;j++)
			   {
				 System.out.print(c[j]);
			   }
			   System.out.print("-");
			   for(int j=4;j<6;j++)
			   {
				 System.out.print(c[j]);
			   }
			   System.out.print("-");
			   for(int j=6;j<8;j++)
			   {
				  System.out.print(c[j]);
			   }
			  System.out.println();
              }
            }
        	
        	else if(xuan.equals("sort2"))
            {
        	  
        	  for(int i=0;i<n;i++)
        	  {
        	     birth[i]=shen[i].substring(6, 14);
        	     shuzi[i]=Long.parseLong(birth[i]);
              }
        	  Arrays.sort(shuzi);
        	  for(int i=0;i<n;i++)
        	  {
        		 hui[i]=Long.toString(shuzi[i]);
        	  }
        	  for(int i=0;i<n;i++)
        	  {
        	    for(int j=0;j<n;j++)
        	    {
        		  if(birth[j].equals(hui[i]))
        		  {
        			System.out.println(shen[j]);
        		  }
        	    }
        	  }
	        }
        	
	    }
        System.out.println("exit");
	}
}
